package com.atguigu.core.acc

import org.apache.spark.broadcast.Broadcast
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object TestBroadcast {
  def main(args: Array[String]): Unit = {

    // 初始化sc
    val conf: SparkConf = new SparkConf().setAppName("WordCount").setMaster("local[*]")
    val sc = new SparkContext(conf)
    sc.setLogLevel("ERROR")

    val rdd: RDD[String] = sc.makeRDD(List("atguigu", "baidu", "tenxun"))

    val temp = "guigu"
    val str: Broadcast[String] = sc.broadcast(temp)

    /**
     * 累加器和广播变量的区别：
     * 都是共享变量
     * 累加器只能写
     * 广播变量只能读
     */

    val result = rdd.filter(x => {
      x.contains(str.value)
    })


    result.foreach(println(_))

    sc.stop()

  }

}
